package com.toplabs.unashu.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import com.toplabs.unashu.dto.PageDto;
import com.toplabs.unashu.entity.Type;
import com.toplabs.unashu.mapper.TypeMapper;
import com.toplabs.unashu.service.TypeService;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ityanger
 * @since 2020-08-23
 */
@Service
@Transactional
public class TypeServiceImpl implements TypeService {
    public static final String TYPE_LIST = "TypeList";
    public static final String TYPE_LIST_COUNT = "TypeListCount";
    private final StringRedisTemplate stringRedisTemplate;
    private final TypeMapper typeMapper;

    public TypeServiceImpl(TypeMapper typeMapper, StringRedisTemplate stringRedisTemplate) {
        this.typeMapper = typeMapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public int insertType(Type type) {
        return typeMapper.insertType(type);
    }

    @Override
    public Type selectType(Type type) {
        return typeMapper.selectType(type);
    }

    @Override
    public List<Type> listType() {
        String s = stringRedisTemplate.opsForValue().get(TYPE_LIST);
        if (!CharSequenceUtil.isEmpty(s)) {
            return JSONUtil.toList(JSONUtil.parseArray(s), Type.class);
        }
        List<Type> types = typeMapper.listType();
        stringRedisTemplate.opsForValue().set(TYPE_LIST, JSONUtil.toJsonStr(types));
        return types;
    }

    @Override
    public PageDto<Type> listTypeBlogCount(Integer pageNum, Integer pageSize) {
        List<Type> types;
        int countType = 0;
        if (pageNum == null) {
            String s = stringRedisTemplate.opsForValue().get(TYPE_LIST_COUNT);
            if (!CharSequenceUtil.isEmpty(s)) {
                types = JSONUtil.toList(JSONUtil.parseArray(s), Type.class);
            } else {
                types = typeMapper.listTypeBlogCount(null, null);
                stringRedisTemplate.opsForValue().set(TYPE_LIST_COUNT, JSONUtil.toJsonStr(types));
            }
        } else {
            types = typeMapper.listTypeBlogCount((pageNum - 1) * pageSize, pageSize);
            countType = typeMapper.countType();
        }
        return new PageDto<>(countType, types);
    }

    @Override
    public PageDto<Type> listTypeBlog(Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public void updateType(Type type) throws NotFoundException {
        int update = typeMapper.updateType(type);
        if (update == 0) {
            throw new NotFoundException("不存在该类型");
        }
        Set<String> keys = stringRedisTemplate.keys("BlogDetailed:*");
        Set<String> keys1 = stringRedisTemplate.keys("BlogCount:*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        if (keys1 != null) {
            stringRedisTemplate.delete(keys1);
        }
        stringRedisTemplate.delete(TYPE_LIST);
        stringRedisTemplate.delete(TYPE_LIST_COUNT);

    }

    @Override
    public int deleteType(Long id) {
        typeMapper.deleteBlogType(id);
        Set<String> keys = stringRedisTemplate.keys("BlogDetailed:*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        stringRedisTemplate.delete(TYPE_LIST);
        stringRedisTemplate.delete(TYPE_LIST_COUNT);
        return typeMapper.deleteType(id);
    }
}
